/* eslint-disable no-param-reassign,no-unused-expressions */
import { Message } from 'element-ui'
import { setCookie, delCookie } from '@/utils/cookie'
import {
  userLogin,
  getUser,
  userLogout,
  getUserInfo,
  validToken
} from '@/service/login'

export default {
  namespaced: true,
  state: {
    token: 'fakeToken',
    user: null,
    login: true,
    loginByAppToken: false, // 通过APPToken登录
    entPermissionsList: [],
    isIframe: window !== window.top // 页面以iframe形式嵌入到别的页面中, 此时不能使用cookie, 请求头用上面的token, 不走cookie
  },
  actions: {
    // 用户登录
    async userLogin(store, params) {
      delCookie('token')
      return userLogin(params)
        .then((res) => {
          setCookie('token', res.data)
          store.commit('setToken', res.data)
          // store.dispatch('getUser')
          return res
        })
        .catch(({ message, code }) => {
          message && Message.error(message)
          return code
        })
    },
    async getUser({ state }, params) {
      return getUser(params)
        .then((res) => {
          if (res && +res.code === 0) {
            state.user = res.data
          }
          return res
        })
        .catch(({ message, code }) => {
          message && Message.error(message)
          return code
        })
    },
    // 用户登出
    async userLogout(store, params) {
      return userLogout(params)
        .then(() => {
          delCookie('token')
          window.location.href = `${window.location.origin}${window.location.pathname}#/login`
          return true
        })
        .catch(({ message }) => {
          message && Message.error(message)
          return false
        })
    },
    // 获取基本用户信息
    async getUserInfo(store) {
      //  if (store.state.loading) return true;
      // store.state.loading = true;
      return getUserInfo()
        .then(({ data, user, permissions_json, entPermissionsList = [] }) => {
          if (!user) {
            throw new Error('获取用户信息失败或暂无权限！')
          }
          store.commit('setEntPermissionsList', entPermissionsList)
          store.commit('setUser', user)
          store.state.entPermissionsList = entPermissionsList
          return { entPermissionsList, user }
        })
        .catch(({ message, code }) => {
          code !== 401 && message && Message.error(message)
          store.state.loading = false
          return false
        })
    },
    async validToken(store, params) {
      return validToken(params)
        .then((res) => res)
        .catch(({ message }) => {
          console.log(message)
          return false
        })
    }
  },
  mutations: {
    updateLogin(state, login) {
      if (login === false) {
        state.token = ''
        state.user = null
      }
      state.login = login
    },

    setUser(state, data) {
      state.user = data
    },

    setToken(state, data) {
      state.token = data
    },

    setEntPermissionsList(state, data) {
      state.entPermissionsList = data
    },
    setLoginByAppToken(state, flag = false) {
      state.loginByAppToken = flag
    }
  }
}
